<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="合作商搜索">
          <el-input v-model="formInline.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="合作商名称" prop="name" />
        <el-table-column label="账号" prop="account" />
        <el-table-column label="设备数量" prop="vmCount" />
        <el-table-column label="分成比例" prop="ratio" />
        <el-table-column
          label="联系人"
          prop="contact"
        />
        <el-table-column label="联系电话" prop="mobile" />
        <el-table-column label="操作" width="220">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="resetPassword(row)"
            >重置密码</el-button>
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >查看详情</el-button>
            <el-button
              type="text"
              size="small"
              @click="editFn(row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row v-if="totalPage!==1" type="flex" justify="space-between" align="middle">
        <el-col :span="19">
          <p class="paginationHeader">
            {{ `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页` }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!-- 合作商详情 -->
    <DetailPartner :details-visible="detailsVisible" :item="parentObj" @closeDialog="closeDialog" />
    <!-- 新增合作商表单 -->
    <AddPartner :item="addPartnerObj" :dialog-partner="dialogPartner" @PartnerDialog="PartnerDialog" @addPartner="addPartnerFn" />
  </div>
</template>

<script>
import AddPartner from './component/add-partner.vue'
import DetailPartner from './component/detail-partner.vue'
import { Partnersearch, updataPsw } from '@/api/node'
export default {
  components: {
    DetailPartner,
    AddPartner
  },
  data() {
    return {
      formInline: { name: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,
      totalPage: 0,
      detailsVisible: false,
      parentObj: {},
      dialogPartner: false, // 新增合作商表单显示和隐藏
      addPartnerObj: {}// 修改合作商的数据回写
    }
  },
  created() {
    this.Partnersearch()
  },
  methods: {
    // 表格数据
    async Partnersearch(name) {
      this.loading = true
      const { data } = await Partnersearch({ ...this.page, ...name })
      this.list = data.currentPageRecords
      this.loading = false
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.Partnersearch(this.formInline)
    },
    // 2.新增合作商
    addForm() {
      this.dialogPartner = true
    },
    // 2.1.关闭新增合作商
    PartnerDialog() {
      this.addPartnerObj = {}
      this.dialogPartner = false
    },
    // 2.2.新增完毕触发父组件关闭弹出层--重新向表格拉去新数据
    addPartnerFn() {
      this.Partnersearch()
      this.addPartnerObj = {}
      this.dialogPartner = false
    },
    // 2.3.修改合作商和新增公用一个
    editFn(row) {
      this.addPartnerObj = row
      this.dialogPartner = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.Partnersearch()
    },
    // 4.查看详情
    detailsFn(row) {
      this.parentObj = row
      this.detailsVisible = true
    },
    // 4.1.关闭查看详情
    closeDialog() {
      this.detailsVisible = false
    },
    // 5.重置密码
    resetPassword(row) {
      this.$confirm('确定要重置合作商密码吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        showClose: false
      })
        .then(async() => {
          await updataPsw(row.id)
          this.Partnersearch()
          this.$message.success('重置密码成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss">
.el-message-box__wrapper {
  .el-message-box {
    width: 380px;
    // padding: 0 1px 40px 17px;
    .el-message-box__status.el-icon-warning {
      color: #ff665f;
    }
    .el-message-box__btns {
      text-align: center;
      margin-top: 10px;
    }
    .el-message-box__message {
      font-size: 16px;
      color: #000;
      font-weight: 500;
    }
    .el-button--primary {
      background-color: #5f84ff;
      border: unset;
    }
  }
}
</style>
